package com.cdbkj.cloud.pos.utils;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.WindowManager;

import com.cdbkj.cloud.pos.MainActivity;
import com.cdbkj.cloud.pos.R;
import com.cdbkj.cloud.pos.bean.Filed;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Method;


/**
 * Created by luozongsheng on 22/06/2018.
 */

public class Tools {

    private static CustomAlertDialog dialog;
    private static CustomImgDialog imgDialog;
    private static CustomPwdDialog pwdDialog;
    private static AlertDialog alertDialog;
    public static int retentionTime = Filed.retentionTime;//页面滞留时间

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public static void backgroundAlpha(Activity activity,float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        // 0.0-1.0
        lp.alpha = bgAlpha;
        activity.getWindow().setAttributes(lp);
    }

      public static void showDialog(String msg,final Activity activity,View.OnClickListener _onCancelClick,View.OnClickListener _onSureClick){
        Tools.backgroundAlpha(activity,0.9f);
        dialog = new CustomAlertDialog(activity, R.style.random_code_dialog);
        dialog.setText("系统提示",msg,"取消","确定");
        dialog.show();
        dialog.setLeftOnclick(_onCancelClick);
        dialog.setRightOnclick(_onSureClick);
        //dialog.setCanceledOnTouchOutside(false);//调用这个方法时，按对话框以外的地方不起作用。按返回键还起作用
        dialog.setCancelable(false);//调用这个方法时，按对话框以外的地方不起作用。按返回键也不起作用
    }

    public static void alertDialog(String msg,final Activity activity){
        pwdDialog.hide();
        Tools.backgroundAlpha(activity,0.9f);
        alertDialog = new AlertDialog(activity, R.style.random_code_dialog);
        alertDialog.setText(msg);
        alertDialog.show();
        //dialog.setCanceledOnTouchOutside(false);//调用这个方法时，按对话框以外的地方不起作用。按返回键还起作用
        alertDialog.setCancelable(false);//调用这个方法时，按对话框以外的地方不起作用。按返回键也不起作用
    }

    public static void closeAlertDialog(){
        alertDialog.hide();
    }

    public static void showDialog(String msg,final Activity activity){
        Tools.backgroundAlpha(activity,0.9f);
        dialog = new CustomAlertDialog(activity, R.style.random_code_dialog);
        dialog.setText("系统提示",msg,"取消","确定");
        dialog.show();
        dialog.setLeftOnclick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.setRightOnclick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        //dialog.setCanceledOnTouchOutside(false);//调用这个方法时，按对话框以外的地方不起作用。按返回键还起作用
        dialog.setCancelable(false);//调用这个方法时，按对话框以外的地方不起作用。按返回键也不起作用
    }

    public static void showDialogWithReturnMainActivity(String msg,final Activity activity){
        Tools.backgroundAlpha(activity,0.9f);
        dialog = new CustomAlertDialog(activity, R.style.random_code_dialog);
        dialog.setText("系统提示",msg,"取消","确定");
        dialog.show();
        dialog.setLeftOnclick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                Tools.retentionTime = 0;
                Tools.returnMainActivity(activity);
            }
        });
        dialog.setRightOnclick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                Tools.retentionTime = 0;
                Tools.returnMainActivity(activity);
            }
        });
        //dialog.setCanceledOnTouchOutside(false);//调用这个方法时，按对话框以外的地方不起作用。按返回键还起作用
        dialog.setCancelable(false);//调用这个方法时，按对话框以外的地方不起作用。按返回键也不起作用
    }

    public static void showImgDialog(String title,String imgUrl,final Activity activity){
        Tools.backgroundAlpha(activity,0.9f);
        imgDialog = new CustomImgDialog(activity, R.style.random_code_dialog);
        imgDialog.setText(title);
        imgDialog.setImage(activity,imgUrl);
        imgDialog.show();
        imgDialog.setCancelable(false);
    }

    public static void showPwdDialog(final Activity activity,View.OnClickListener _onSureClick){
        Tools.backgroundAlpha(activity,0.9f);
        pwdDialog = new CustomPwdDialog(activity, R.style.random_code_dialog);
        pwdDialog.show();
        pwdDialog.setLeftOnclick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pwdDialog.dismiss();
                Tools.retentionTime = 0;
                Tools.returnMainActivity(activity);
            }
        });
        pwdDialog.setRightOnclick(_onSureClick);
        pwdDialog.setCancelable(false);
    }

    public static void closePwdDialog(final Activity activity){
        pwdDialog.hide();
    }

    public static String getPwdDialogPwd(){
        if(pwdDialog!=null){
            return pwdDialog.getPwd();
        }
        return "";
    }

    public static void dismissDialog(){
        dialog.dismiss();
    }

    public static void dismissImgDialog(){
        imgDialog.dismiss();
    }

    public static void returnMainActivity(Activity activity){
        Intent intent = new Intent(activity,MainActivity.class);
        activity.startActivity(intent);
    }

    public static String stringToJson(String s) {
        return s.replaceAll("\\\\", "").replaceAll("\"\\{","\\{").replaceAll("\\}\"","\\}").replaceAll("&quot;","\"");
    }

    public static  synchronized JSONObject getJsonObject(String JsonString)
    {
        JSONObject jsonObject = null;
        try {
            JsonString = getJsonStrFromNetData(JsonString);
            JSONArray entries = new JSONArray(JsonString);
            if(entries.length() > 0 )
            {
                jsonObject = entries.getJSONObject(0);
            }
            return jsonObject;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @summary 去除非Json的字符串部分
     * @param jsonString 包含Json字符串的数据
     * @return json字符串
     */
    public static  synchronized String getJsonStrFromNetData(String jsonString)
    {
        int first = jsonString.indexOf("[");
        int last = jsonString.lastIndexOf("]");
        String result = "";
        if (last > first) {
            result = jsonString.substring(first, last + 1);
        }
        return result;
    }

    /**
     * 动态获取产品序列号sn 系统识别号
     */
    static Method systemProperties_get = null;
    public static String getAndroidOsSystemProperties() {
        String ret;
        try {
            systemProperties_get = Class.forName("android.os.SystemProperties").getMethod("get", String.class);
            //"ro.boot.serialno"、"ro.serialno"
            if ((ret = (String) systemProperties_get.invoke(null, "ro.serialno")) != null)
                return ret;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return "";
    }
}
